<?php

/**
 * This is the model class for table "cargo_ref".
 */
class cargo_ref extends ActiveRecord
{
	/**
	 * The followings are the available columns in table 'cargo_ref':
	 * @var integer $id
	 * @var integer $cargo_id
	 * @var integer $date
	 * @var string $ref_no
	 * @var string $ref_type
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return cargo_ref the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cargo_ref';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ref_no, ref_type', 'required'),
			array('cargo_id, date', 'numerical', 'integerOnly'=>true),
			array('ref_no', 'length', 'max'=>25),
			array('ref_type', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, cargo_id, date, ref_no, ref_type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function beforeValidate(){
		$this->date = time();
		return parent::beforeValidate();
	}
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
	public function Cargo($id=0){
    	$this->getDbCriteria()->mergeWith(array(
        	'condition'=>'cargo_id=:cargo_id',
        	'params'=>array('cargo_id'=>$id),
    	));
    	return $this;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'cargo_id' => 'Cargo',
			'date' => 'Date',
			'ref_no' => 'Ref No',
			'ref_type' => 'Ref Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('date',$this->date);

		$criteria->compare('ref_no',$this->ref_no,true);

		$criteria->compare('ref_type',$this->ref_type,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function save(){
		$criteria = new CDbCriteria;
		$criteria->compare('ref_no',$this->ref_no,false);
		$criteria->compare('ref_type',$this->ref_type,false);
		$criteria->compare('cargo_id',$this->cargo_id,false);
		if($this->exists($criteria))
			return true;
		if(!empty($this->ref_no))
			return parent::save();
		else
			return true;
	}
}